<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- k个数相加等于n的数(数字不能重复) -->
    <script>
      var combinationSum3 = function (k, n) {
        let result = [] //存储输出答案
        let path = [] //存储单次的结果
        backtracking(1, path, 0) //调用递归函数
        function backtracking(start, path, sum) {
          if (path.length === k) {
            //单次结果长度等于 k 时
            if (sum === n) {
              //和一样的话 ，放进输出结果中
              result.push([...path])
            }
            return
          }
          for (let i = start; i <= 9; i++) {
            //进行递归，将一个个值放进 path 中
            path.push(i)
            backtracking(i + 1, path, sum + i)
            path.pop()
          }
        }
        return result
      }
      console.log(combinationSum3((k = 2), (n = 10)))
    </script>
  </body>
</html>
